<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - FormClass"
*/ 

component 
	displayname="public.service.db.senate.sequence.FormClass"  
	output=false 
	accessors=true 
	{
	property type="string" name="SubjectID" hint="专业";
	property type="string" name="Grade" hint="年级";
	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		var nextID = left(getSubjectID() & "0000", 6) & right(getGrade(), 2) & numberFormat(1, "0_");

		var sql = "SELECT a.cls_id 
					FROM t_class a  
					WHERE 
						a.sbj_id = :subject 
						AND
						a.grade = :grade 
						AND
						a.cls_id LIKE :condition 
					ORDER BY a.cls_id DESC";

		var queryObj = new Query( datasource = application.dnsMaster, maxRows = 1);
		queryObj.addParam( name="subject", value=getSubjectID(), cfsqltype="cf_sql_varchar" );		
		queryObj.addParam( name="grade", value=getGrade(), cfsqltype="cf_sql_char" );	
		queryObj.addParam( name="condition", value=left(getSubjectID() & "0000", 6) & right(getGrade(), 2) & "__", cfsqltype="cf_sql_char" );	
		var rs_check = queryObj.execute( sql=sql ).getResult();
		/* 
			主键生成规则 
			
			班级代码为10位 前6位为专业(或专业方向代码) +  年级代码后2位 + 2位流水号
			
			若班级是划分至专业 则前5位专业代码 + 0
			若班级是划分至专业方向 则前6位专业方向代码
		*/
		
		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = left(getSubjectID() & "0000", 6) & right(getGrade(), 2) & numberFormat((right(rs_check.cls_id, 2) + 1), "0_");
			
		}
		
		return nextID;
	}

}

</cfscript>